package com.cy.shop.service.impl;

import com.cy.shop.VO.CartVO;
import com.cy.shop.entity.Cart;
import com.cy.shop.entity.Product;
import com.cy.shop.mapper.CartMapper;
import com.cy.shop.mapper.ProductMapper;
import com.cy.shop.service.ICartService;
import com.cy.shop.service.ex.AccessDeniedException;
import com.cy.shop.service.ex.CartNotFoundException;
import com.cy.shop.service.ex.InsertException;
import com.cy.shop.service.ex.UpdateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class CartServiceImpl implements ICartService {

    /*购物车的业务层依赖于购物车的持久层和商品的持久层*/
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public void addToCart(Integer uid, Integer pid,
                          Integer amount, String username) {
        //当前要添加的商品是否在购物车中以及存在
        Cart result = cartMapper.findByUidAndPid(uid,pid);
        Date date = new Date();
        if (result == null){  //为null时表示这个商品从未添加到购物车当中,则进行新增操作
            /*创建一个cart对象*/
            Cart cart = new Cart();
            /*补全数据*/
            cart.setUid(uid);
            cart.setPid(pid);
            cart.setNum(amount);
            /*补全价格：来自于商品当中的数据*/
            Product product = productMapper.findById(pid);
            cart.setPrice(product.getPrice());
            /*补全四个日志*/
            cart.setCreatedUser(username);
            cart.setCreatedTime(date);
            cart.setModifiedUser(username);
            cart.setModifiedTime(date);

            /*执行数据的插入操作*/
            Integer rows = cartMapper.insert(cart);
            if (rows != 1){
                throw new InsertException("插入数据时产生未知的异常");
            }
        }else { //反之则表示当前的商品在购物车中已经存在，更新这个商品的num即可
            /*进行原有商品个数和新增商品个数进行相加操作（前端、后端业务层）*/
            Integer num = result.getNum() + amount;
            Integer rows = cartMapper.updateNumCid(result.getCid(),
                                    num,
                                    username,
                                    date);
            if (rows != 1){
                throw new UpdateException("更新数据时产生未知异常");
            }
        }
    }

    @Override
    public List<CartVO> getVOByUid(Integer uid) {
        return cartMapper.findVOByUid(uid);
    }

    @Override
    public Integer addNum(Integer cid, Integer uid, String username) {
        Cart result = cartMapper.findByCid(cid);
        if (result == null){
            throw new CartNotFoundException("购物车中数据不存在");
        }
        if (!result.getUid().equals(uid)){
            throw new AccessDeniedException("数据非法访问");
        }
        Integer num = result.getNum() + 1;
        Integer rows = cartMapper.updateNumCid(cid, num, username, new Date());
        if (rows != 1){
            throw new UpdateException("更新数据失败");
        }
        //返回新的购物车数据的总量
        return num;
    }

    @Override
    public List<CartVO> getVOByCid(Integer uid, Integer[] cids) {
        List<CartVO> list = cartMapper.findVOByCid(cids);
        Iterator<CartVO> it = list.iterator();
        while (it.hasNext()){
            CartVO cartVO = it.next();
            if (!cartVO.getUid().equals(uid)){ //表示当前数据不属于当前用户
                //从集合中移除这个元素
                list.remove(cartVO);
            }
        }
        return list;
    }
}
